﻿using System;

using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class FareSegments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["fare_code"] != null)
                {
                    lblAirline.Text = Session["AIRLINE"].ToString();
                    lblFareCode.Text = Request.QueryString["fare_code"];
                    if (Request.QueryString["cabin"] == null)//从待审批队列和子舱可用产品链进来时，只允许修改价格，不允许新增航段，因为信息不全！
                    {
                        panAdd.Visible = false;
                    }
                    else
                    {
                        lblCabin.Text = Request.QueryString["cabin"].Trim();
                        lblOpAirline.Text = Request.QueryString["op_airline"];
                        panAdd.Visible = true;
                    }
                        
                }
                else
                {
                    lblError.Text = "Illegal page call!";
                    lblFareCode.Text = "";
                }
                if (Session["ACL"].ToString().IndexOf("FA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (lblCabin.Text == "*")//从带审批队列链进来时，只允许修改价格，不允许新增航段，因为信息不全！
            {
                btnAdd.Enabled = false;
                return;
            }

            if (Session["ACL"].ToString().IndexOf("FA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            if (lblFareCode.Text == "")
            {
                lblError.Text = "Please select a fare.";
                return;
            }
            txtOriAirport.Text = txtOriAirport.Text.Trim().ToUpper();
            txtDesAirport.Text = txtDesAirport.Text.Trim().ToUpper();
            StringBuilder week_mask = new StringBuilder(8);
            if (chkWeek1.Checked)
                week_mask.Append("1");
            else
                week_mask.Append("_");
            if (chkWeek2.Checked)
                week_mask.Append("2");
            else
                week_mask.Append("_");
            if (chkWeek3.Checked)
                week_mask.Append("3");
            else
                week_mask.Append("_");
            if (chkWeek4.Checked)
                week_mask.Append("4");
            else
                week_mask.Append("_");
            if (chkWeek5.Checked)
                week_mask.Append("5");
            else
                week_mask.Append("_");
            if (chkWeek6.Checked)
                week_mask.Append("6");
            else
                week_mask.Append("_");
            if (chkWeek7.Checked)
                week_mask.Append("7");
            else
                week_mask.Append("_");
            try
            {
                SqlCommand cmd = new SqlCommand("bll_fare_add_segment");
                SqlParameter para = cmd.Parameters.AddWithValue("@fare_code", lblFareCode.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@seg_sn", lstSegNo.Text);
                para = cmd.Parameters.AddWithValue("@ori_airport", txtOriAirport.Text);
                para = cmd.Parameters.AddWithValue("@des_airport", txtDesAirport.Text);
                para = cmd.Parameters.AddWithValue("@week_mask", week_mask.ToString());
                para = cmd.Parameters.AddWithValue("@class", lstClass.Text);
                para = cmd.Parameters.AddWithValue("@stay_min", lstStayMin.Text);
                para = cmd.Parameters.AddWithValue("@stay_max", txtStayMax.Text);
                //para = cmd.Parameters.AddWithValue("@adt_surcharge", txtAdtCharge.Text);
                //para = cmd.Parameters.AddWithValue("@chd_surcharge", txtChdCharge.Text);
                //para = cmd.Parameters.AddWithValue("@inf_surcharge", txtInfCharge.Text);
                //para = cmd.Parameters.AddWithValue("@tax_currency", lstCurrencies.SelectedValue);
                //para = cmd.Parameters.AddWithValue("@adt_taxes", txtAdtTaxes.Text);
                //para = cmd.Parameters.AddWithValue("@chd_taxes", txtChdTaxes.Text);
                //para = cmd.Parameters.AddWithValue("@inf_taxes", txtInfTaxes.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Fare Not Found!";
                else if (ret_code == 2)
                    lblError.Text = "Invalid segment number!";
                else if (ret_code == 4)
                    lblError.Text = "Products about this segment are exceed system limit(200)!";
                else if (ret_code == 5)
                    lblError.Text = "Airline not found!";
                else if (ret_code == 6)
                    lblError.Text = "Max fare product segments limits to 2!";
                else if (ret_code == 7)
                    lblError.Text = "Max fare product segments limited to 4!";
                else if (ret_code == 8)
                    lblError.Text = "First segments not found!";
                else if (ret_code == 9)
                    lblError.Text = "Only return trip was acceptable!";
                else if (ret_code == 10)
                    lblError.Text = "Adding failed!";
                else if (ret_code == 11)
                    lblError.Text = "Ori. airport not found!";
                else if (ret_code == 12)
                    lblError.Text = "Des. airport not found!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            //string key;
            //string[] strArray;
            //if (Request.Form["KEY"] == null)
            //    return;
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_fare_delete_segment";
                SqlParameter para = cmd.Parameters.AddWithValue("@fare_code", lblFareCode.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@seg_sn", Request.Form["KEY"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "deleting failed!";
                else if (ret_code == 2)
                    lblError.Text = "Fare not found or not proper status!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnPriceStairs_Click(object sender, EventArgs e)
        {
            //string key;
            //string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            //key = Request.Form["KEY"];
            //strArray = key.Split('|');
            Response.Redirect("FareStairs.aspx?fare_code=" + lblFareCode.Text + "&airline=" + lblAirline.Text + "&seg_sn=" + Request.Form["KEY"].ToString());
        }
    }
}